package org.ands.apps;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ands.client.bom.ContentsRetrieverBOM;
import org.ands.data.Contents;
import org.ands.rifcs.RifCsNamespaceContext;
import org.ands.xml.DOMHelper;

public class ExtractSpatialKML {
	
	public static void main(String[] args) {
		
		if (args.length != 1){
			System.err.println("Requires path to directory");
			System.exit(0);
		}
		
		final String directoryPath = args[0];
		if((directoryPath == null) || (directoryPath.isEmpty())){
			System.err.println("Requires valid directory path");
			System.exit(0);
		}
		
		try{
		
			File directory = new File(directoryPath);
			assert(directory.isDirectory());
			if(!directory.isDirectory()){
				System.err.println(directoryPath+" is not a directory");
				System.exit(0);
			}
			
			ContentsRetrieverBOM contentsRetrieverBOM = new ContentsRetrieverBOM();
			DOMDocumentConstructor domDocumentConstructor = new DOMDocumentConstructor();
			
			Map<Integer, List<Contents>> xmlContentsSpatialAll = new HashMap<Integer, List<Contents>>();
			List<Contents> topContentsList = new ArrayList<Contents>();
			topContentsList.add(new Contents("/registryObjects", ""));
			xmlContentsSpatialAll.put(0, topContentsList);
			
		
			File[] files = new File(directoryPath).listFiles();
			int fileCount = 1;
			for (File file : files) {
		        if (file.isDirectory()) {
		            System.out.println("Skipping directory " + file.getName());
		            
		        } else {
		        	if(file.getName().endsWith(".kml")){
		        		System.out.println("Processing kml file: " + file.getName());
		        		
		        		Map<Integer, List<Contents>> xmlContentsSpatialPerKML = contentsRetrieverBOM.getSpatialFromKML(file.getName(), fileCount++);
		        		if(xmlContentsSpatialPerKML.isEmpty()){
		        			throw new Exception("Requires non-empty list of XMLContents");
		        		}
		        		
		        		xmlContentsSpatialAll.putAll(xmlContentsSpatialPerKML);
		        	}
		        	else{
		        		System.out.println("Ignoring non kml file: " + file.getName());
		        	}
		        }
		    }
			
			Map<String, String> docRootAttributeMap = new HashMap<String, String>();
			
			docRootAttributeMap.put("xmlns", "http://ands.org.au/standards/rif-cs/registryObjects");
			docRootAttributeMap.put("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
			docRootAttributeMap.put("/registryObjects[1]/@xsi:schemaLocation", "http://ands.org.au/standards/rif-cs/registryObjects");

			RifCsNamespaceContext namespaceContext = new RifCsNamespaceContext();
			DOMHelper domHelper = domDocumentConstructor.documentFromXMLContents(namespaceContext, "http://ands.org.au/standards/rif-cs/registryObjects", "registryObjects", docRootAttributeMap, xmlContentsSpatialAll);
			domHelper.writeToFile("SpatialRifCSFromKML.xml");
			
			
		} catch (Exception e) {
			System.err.println("Exception ["+e.getMessage()+"].");
			e.printStackTrace();
		}
	}
}
